<!DOCTYPE html>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理者页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/bootstrap-theme.min.css" />
<link rel="stylesheet" type="text/css" href="${contextPath}/css/public.css">
<link href="${contextPath}/css/jquery-ui.min.css" rel="stylesheet">
<link href="${contextPath}/css/jquery-ui.theme.min.css" rel="stylesheet">
<style type="text/css">
	.input-group-addon,.form-control{padding:6px;}
	.ui-datepicker .ui-datepicker-title select{color:#1c94c4}
	.form-btn1{height: 50px;float: left;}
</style>
<script type="text/javascript" src="${contextPath}/js/jquery.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/bootstrap.min.js"></script>
<script type="text/javascript" src="${contextPath}/js/common.js"></script>
<!-- cx20150108 -->
<script type="text/javascript" src="${contextPath}/js/jquery.ui.core.js"></script>
<script type="text/javascript" src="${contextPath}/js/jquery.ui.widget.js"></script>
<script type="text/javascript" src="${contextPath}/js/jquery.ui.datepicker.js"></script>
<script type="text/javascript" src="${contextPath}/js/jquery.ui.datepicker-ch.js"></script>
</head>
<body>
	<div class="container">
		<div class="panel panel-default">
			<div class="panel-heading">${model.chooseUserName}推荐的人员信息</div>
			<div class="panel-body">
				<form class="form-inline" role="form">
					<div class="form-group">
						<div class="input-group">
							<div class="input-group-addon">被推荐人用户名：</div>
							<input class="form-control" onkeydown="if(event.keyCode==13){return false;}" id="referralUsernameCondition" type="text" placeholder="用户名">
						</div>
						<div class="input-group">
							<div class="input-group-addon"> 日期：</div>
							<input class="form-control" id="dateCondition" style="width: 100px;" value="" type="text" readonly="readonly">
						</div>
						<button type="button" id="searchBtn" class="btn btn-default">查询</button>
					</div>	
					<div style="float: right;">
						<button type="button" id="addBtn" class="btn btn-success">添加被推荐人</button>
					</div>
				</form>
			</div>
			<table class="table table-striped table-bordered">
				<thead>
					<tr>
						<th width='10%'>序号</th>
						<th width='25%'>用户名</th>
						<th width='25%'>真实姓名</th>
						<th width='25%'>本月标注时间(小时)</th>
						<th width='15%'>本月工资(元)</th>
					</tr>
				</thead>
				<tbody></tbody>
			</table>
			<ul class="pagination"></ul>
		</div>
			<!-------------------------------- 弹出窗口 任务包详细-------------------------------------------------->
	<div id="referralAddModal" class="modal fade">
		<div class="modal-dialog" style="margin-top: 150px;">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal">
						<span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
					</button>
					<h4 class="modal-title">添加被推荐人到${model.chooseUserName}的名下</h4>
				</div>
				<div class="modal-body">
					<div class="panel-body">
						<form class="form-inline" role="form">
							<div class="form-group">
								<div class="input-group">
									<div class="input-group-addon">被推荐人账号</div>
									<input class="form-control" id="referralAddInput" type="text" placeholder="用户名" onkeydown="if(event.keyCode==13){return false;}">
									<input type="hidden" class="form-control" name="userid" id="userid" />
								</div>
							</div>
							<button type="button" id="referralAdd" class="btn btn-default">添加</button>
							<br/>
							<span id="msg"></span>
						</form>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	</div>
	<script type="text/javascript">
		var nowPage = 1;
		var referralUsernameCondition = "";
		var pageTotal = 0;
		var referrerTest;
		var dateCondition = "";
		/*-------------------------*/
		$(document).ready(function(){
		var todayDate = $.datepicker.formatDate( "yy-mm-dd",new Date());
			dateCondition = todayDate;
			$("#dateCondition").val(todayDate);
			
			/*--------------------------------------时间控件-------------------------------------------------------*/
			$("#dateCondition").datepicker(); 
			$("#dateCondition").datepicker('option', 'dateFormat','yy-mm-dd');//set dateFormat 
			
			getViewOfReferrals(1);
			
			$("#searchBtn").click(function(){
				referralUsernameCondition = $("#referralUsernameCondition").val();
				dateCondition = $("#dateCondition").val();
				getViewOfReferrals(1);
			});
			$("#addBtn").click(function(){
				$("#msg").html("");
				$("#referralAddInput").val("");
				$("#referralAddModal").modal('show');
			});
			$("#referralAdd").click(function(){
				var length = $("#referralAddInput").val().length;
				var referrerUsername = $("#referralAddInput").val();
				var thisUser = "${model.chooseUserName}";
				var thisUserReferrerId = "${model.thisUserReferrerId}";
				if(length==0){
					$("#msg").html("");
					$("#msg").css("color","red");
					$("#msg").html("请填写被推荐人账号！");
				}else if(referrerUsername == thisUser){
					$("#msg").html("");
					$("#msg").css("color","red");
					$("#msg").html("请不要填写当前查看工作者账号！");
					$("#userid").val("");
				}else{
					checkReferrerExist(referrerUsername);
					if(thisUserReferrerId!="0"){
						if($("#userid").val()==thisUserReferrerId){
							referrerTest = false;
							$("#msg").html("");
							$("#msg").css("color","red");
							$("#msg").html("请不要填写当前查看工作者推荐人的账号！");
							$("#userid").val("");
						}
					}
					if(referrerTest){
						$.ajax({
							type:'POST',
							data:{"useridToAdd":'${model.userId}',"useridToUpdate":$("#userid").val()},
							url:'${contextPath}/security/addReferral',
							dataType:'json',
							success:function(data){
								if(data.message==1){
									$("#msg").html("");
									$("#msg").css("color","green");
									$("#msg").html("被推荐人添加成功！");
									getViewOfReferrals(1);
								}else{
									$("#msg").html("");
									$("#msg").css("color","red");
									$("#msg").html("被推荐人添加失败，请联系系统管理员！");
								}
							}
						});
					}
				}
			});
			
		});
		getViewOfReferrals = function(pageNum){
			$.ajax({
				type:'POST',
				data:{"userId":'${model.userId}',"page":pageNum,"referralUsernameCondition":referralUsernameCondition,"dateCondition":dateCondition},
				url:'${contextPath}/security/getViewOfReferrals',
				dataType:'json',
				success:function(data){
					$("tbody").empty();
					if(data.list == ""){
						$("tbody").empty();
						$("tbody").append("<tr class='text-danger'><td colspan='7'>无内容</td></tr>");
					}else{
						pageTotal = data.totlePage;
						$.each(data.list,function(i,item){
							
							$("tbody").append(
								"<tr>"+
									"<td>"+(i+1)+"</td>"+
									"<td>"+item.username+"</a></td>"+
									"<td>"+item.realName+"</td>"+
									"<td>"+item.marktime+"</td>"+
									"<td>"+item.salaryValue+"</td>"+
								"</tr>"
							);
							var pageDom = $(".pagination");
							pageDom.empty();
							page.creatPageHTML(pageNum,pageTotal,pageDom,"getViewOfReferrals");
							/*--------------------------------------跳转页-------------------------------------------------------*/
							$(".pageGoBtn").click(function(){
								var pageNum = 0;
								pageNum = $(".pageGoText").val();
								if(pageNum !=0&&0<pageNum&&pageNum<pageTotal+1){
									getViewOfReferrals(pageNum);
								}
							});
						});
					}
				}
			});
		};
		checkReferrerExist = function(referrerUsername){
			$.ajax({
				type:'post',
				async:false,
				url:'${contextPath}/security/checkReferrerExist',
				data:"referrerUsername="+referrerUsername,
				dataType:'json',
				success:function(data){
					if(data.message==2){
						$("#msg").css("color","red").text("");
						$("#userid").val(data.referrerId);
						referrerTest = true;
					}else{
						if(data.message==0){
							$("#userid").val("");
							$("#msg").css("color","red").text("");
							$("#msg").css("color","red").text("被推荐人用户名不存在");
							referrerTest = false;
						}else{
							$("#userid").val("");
							$("#msg").css("color","red").text("");
							$("#msg").css("color","red").text("被推荐人不是工作者，请重新确认。");
							referrerTest = false;
						}
					}
				}
			});
		};
	</script> 
</body>
</html>
